﻿// See https://aka.ms/new-console-template for more information
using LeastSquare;
/// <summary>
/// 最小二乘法，线性回归预测模型
/// </summary>

// 设置一个年份链表
List<double> listyear = new List<double>();
//输入年份
for (int year = 2020; year >= 2003; year--)
{
    listyear.Add(year);
}
//转成数组
double[] list = listyear.ToArray();
//求年份平均值
double ayear = listyear.Average();
//建立gdp的数组，输入数据
double[] gdp =
{
                14687673892882,
                14279937500608,
                13894817549380,
                12310409370894,
                11233276536744,
                11061553079871,
                10475682920597,
                9570406235659,
                8532229986993,
                7551500124197,
                6087163874510,
                5101703073088,
                4594307032667,
                3550342737009,
                2752131773358,
                2285965892364,
                1955347004965,
                1660287965663
            };
//求gdp平均值
double agdp = gdp.Average();
// 求b
double b = getab.getb(list, gdp, ayear, agdp);
// 求a
double a = getab.geta(ayear, agdp, b);
//将2021带入模型求出结果
double x = 2021;
double result = b * x + a;
Console.WriteLine($"{x}年的预测值为：{result}");
//对比真实值求出模型精度
double fact = 17734062645371;
double presicion = -(result - fact) / fact;
Console.WriteLine($"预测精度为{presicion}");
var m = Console.ReadKey();
        
  